package com.lfk.justweengine.utils.crashHandler;

import ohos.aafwk.ability.delegation.AbilityDelegatorRegistry;
import ohos.app.Context;
import org.junit.Assert;
import org.junit.Test;

public class CrashHandlerOhosTest {

    @Test
    public void testGetInstance() {
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.setActivityName("Test");
        crashHandler.setRestartActivity(Context.class);
        Assert.assertNotNull(crashHandler);
    }

    @Test
    public void testInit() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(context);
        Assert.assertTrue(true);
    }

    @Test
    public void testUncaughtException() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(context);
        Thread thread = new Thread();
        Exception aa = new Exception("Test");
        crashHandler.uncaughtException(thread,aa);
        Assert.assertTrue(true);

    }

    @Test
    public void testCollectDeviceInfo() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(context);
        crashHandler.collectDeviceInfo(context);
        Assert.assertTrue(true);
    }

    @Test
    public void testAddCrashListener() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(context);
        TestListenner testListenner = new TestListenner();
        crashHandler.addCrashListener(testListenner);
        Assert.assertTrue(true);
    }

    private class TestListenner implements  AfterCrashListener{
        @Override
        public void AfterCrash() {
            System.out.println("Test");
        }
    }

    @Test
    public void  testGetStackTraceString() {
        Context context = AbilityDelegatorRegistry.getAbilityDelegator().getAppContext();
        CrashHandler crashHandler = CrashHandler.getInstance();
        crashHandler.init(context);
        String str = crashHandler.getStackTraceString(new Exception("Test"));
        Assert.assertNotNull(str);
    }
}